<?php
	include_once("../backend/constant.php");
	session_start();
	
	if(isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
		// last request was more than 30 minutes ago
		session_unset();     // unset $_SESSION variable for the run-time 
		session_destroy();   // destroy session data in storage
	}
	$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
	
	if( !isset($_SESSION["loggedUser"]) || $_SESSION["loggedUser"]==null){
		header("Location:../index.php");
		return;
	}
	
	$connection = new mysqli(dbhost, dblogin, dbpwd, database) or	die("Couldn't connect to database!");
	$connection->set_charset("utf8");
	
	$sql = "select count(u.username) as totalNumberOfUser, au.NumActiveUser
			from users as u,(select count(username) as NumActiveUser from users where active = 1) as au";
	$result = $connection->query($sql) or die("Query failed!");
	$totalUser = 0;
	$activeUser = 0;
	if($result!=false && ($row=$result->fetch_assoc()) ){
		$totalUser = $row["totalNumberOfUser"];
		$activeUser = $row["NumActiveUser"];
	}
	
	$sql = "select count(a.id) as totalNumberOfArticle, spam.NumSpam
			from articles as a, (select count(id) as NumSpam from articles where isSpam = 1) as spam";
	$result = $connection->query($sql) or die("Query failed!");
	$totalArticle = 0;
	$NumOfSpam = 0;
	if($result!=false && ($row=$result->fetch_assoc()) ){
		$totalArticle = $row["totalNumberOfArticle"];
		$NumOfSpam = $row["NumSpam"];
	}
	
	$sql = "select count(c.id) as totalNumberOfComment, spam.NumSpam
			from comments as c, (select count(id) as NumSpam from comments where isSpam = 1) as spam";
	$result = $connection->query($sql) or die("Query failed!");
	$totalComment = 0;
	$NumOfSpamComment = 0;
	if($result!=false && ($row=$result->fetch_assoc()) ){
		$totalComment = $row["totalNumberOfComment"];
		$NumOfSpamComment = $row["NumSpam"];
	}
	
	$connection->close();	
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="admin.css"/>
		<script type='text/javascript' src='../jquery-min.js'></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#btnSearch").click(function(){
					$("#frmSearchUser").submit();
				});
				
				$("#btnReg").click(function(){
					window.location.replace("reg.php");
					/*
						OR we can use
						window.location.href = "";
					*/
				});
				
				$("#btnLogout").click(function(){
					$.ajax({
						url:"backend/user.php",
						cache:false,
						type:'POST',
						data:{ACTION:'LOGOUT'},
						success:function(responseText){
							//alert(responseText);
							
							var rs = $.parseJSON(responseText);
							$('#div_user_pane').fadeOut();
							$('#div_login').fadeIn();
							
						}
					});
					return false;
				});
			});
			
		</script>
		
		<title>VTech blog - A technical blog</title>
	</head>
	<body>
		<div id="container">
			<div id="div_header">
				<div id="div_user_pane">
					<span style="padding-left:30px;color:#dafafa;">Xin chao </span>
					<a href="#" style="text-decoration:none;color:#ffff22;" ><span style="font-weight:bold;"><?php echo $_SESSION["loggedUser"]; ?></span></a>
					<a href="#" style="text-decoration:none;color:#ffdada" id="btnLogout">Logout</a>
				</div>
			</div>
			<div id="div_nav" class="menu">
				<ul>
					<li><a href="../index.php">Home</a></li>
					<li><a href="../funct.php">NMath</a></li>
					<?php
						if(isset($_SESSION["loggedUser"])){
							if($_SESSION["loggedUser"]=='davu' || $_SESSION["loggedUser"]=='admin'|| $_SESSION["loggedUser"]=='huong'){
								print "<li><a href='#'>Administrative</a></li>";
							}
						}
					?>
					<li><a href="../about.php">About me</a></li>
				</ul>
			</div>
			<div id="sidebar">
				<ul style="list-style-type:none">
					<li>Dashboard</li>
					<li><a href="userlst.php">Users</a></li>
					<li><a href="articles.php">Posts</a></li>
					<li><a href="files.php">Files</a></li>
					<li><a href="../about.php">Application</a></li>
				</ul>
			</div>
			<div id="div_main">
				<table class="tbl-data">
					<tbody>
						<tr>
							<td>Total number of User:</td>
							<td><?php print $totalUser; ?></td>
						</tr>
						<tr>
							<td>Panned user:</td>
							<td><?php print ($totalUser - $activeUser); ?></td>
						</tr>
						<tr>
							<td>Total number of article:</td>
							<td><?php print $totalArticle; ?></td>
						</tr>
						<tr>
							<td>Number of spam:</td>
							<td><?php print $NumOfSpam; ?></td>
						</tr>
						<tr>
							<td>Total of comments:</td>
							<td><?php print $totalComment; ?></td>
						</tr>
					</tbody>
				</table>
			</div>
			<div id="div_footer" >Copyright 2013 @Dao Anh Vu</div>
		</div>
	</body>
</html>
